﻿using System;
using System.Collections.Generic;

using System.Text;
using HZ.Data;
namespace HZ.DrugTrade.DAL
{
    public class DAL_PartDistributionLog
    {
        /// <summary>
        /// 增加分配送
        /// </summary>
        /// <param name="loglist"></param>
        /// <returns>0添加失败，1添加成功，2数据重复</returns>
        public static int AddPartDistributionLog(IList<HZ.DrugTrade.Model.PartDistributionLog> loglist)
        {
            int isOK = 0;
           var sh =  HZ.Data.DbHelperFactory.Create();
            try
            {
                sh.BeginTrans();
                isOK = 1;
                foreach (var item in loglist)
                {

                    //string sql = HZ.Data.DAL_Helper.GetInsertSql(item);
                    //sh.ExecuteNonQuery(sql);
                    HZ.Data.DAL_Helper.OperateTableSQL<HZ.DrugTrade.Model.PartDistributionLog>(item,HZ.Data.TableOperator.Insert,sh);

                    // 查找当前配送是否存在
                    string sql2 = string.Format("SELECT COUNT(*) FROM TB_AreaDistribution WHERE ProcureCatalogID = '{0}' AND AreaID = '{1}' AND  CompanyID_PS = '{2}'", item.ProcureCatalogID, item.AreaID, item.NowCompanyID);
                    int count = int.Parse(sh.ExecuteScalar(sql2).ToString());

                    if (count == 0)
                    {
                        HZ.DrugTrade.Model.AreaDistribution ad = new HZ.DrugTrade.Model.AreaDistribution() { AreaID = item.AreaID, ProcureCatalogID = item.ProcureCatalogID, CompanyID_PS = item.NowCompanyID, CompanyID_Main = item.LastCompanyID };
                        //sh.ExecuteNonQuery(HZ.Data.DAL_Helper.GetInsertSql(ad));
                        HZ.Data.DAL_Helper.OperateTableSQL<HZ.DrugTrade.Model.AreaDistribution>(ad, HZ.Data.TableOperator.Insert, sh);

                    }
                    else
                    {
                        isOK = 2;
                    }
                }
                sh.CommitTrans();
            }
            catch (Exception ee)
            {
                HZ.Common.log4netManager.LogDebug("DAL_PartDistributionLog", ee.Message, ee);
                sh.RollTrans();
#if DEBUG
                throw ee;
#endif
            }

            return isOK;
        }

        /// <summary>
        /// ?????这是干什么的
        /// </summary>
        /// <param name="where"></param>
        /// <param name="fathercompanyID"></param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public static IList<HZ.DrugTrade.Model.View.Distribution> GetPartDistribution(HZ.DrugTrade.Model.Query.Distribution where, string fathercompanyID, int currentpage, int pagesize, out int totalrecord)
        {
            string sql = string.Format(@"SELECT v.* FROM View_Distribution AS v INNER JOIN TB_PartDistributionLog AS t
ON v.CompanyID_PS = t.NowCompanyID WHERE t.LastCompanyID = {0} {1}", fathercompanyID, HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition(where,true));
            HZ.Data.PageDescribe des = new HZ.Data.PageDescribe(sql, pagesize);
            des.CurrentPage = currentpage;
            totalrecord = des.TotalCount;
            return HZ.Data.DAL_Helper.CommonFillList<HZ.DrugTrade.Model.View.Distribution>(des.GetSpecailPage());
        }

        /// <summary>
        /// 获取医疗机构某采购目录的可用的配送企业
        /// </summary>
        /// <param name="ProcureCatalogID"></param>
        /// <param name="HospitalID"></param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public static IList<HZ.DrugTrade.Model.View.AreaDistribution> GetPartDistribution(string ProcureCatalogID, string HospitalID, int currentpage, int pagesize, out int totalrecord)
        {
            string sql = string.Format(@"SELECT * FROM  View_AreaDistribution 
where  Unioncitystate = 1 and ProcurecatalogID ='{0}' and (areaid = (select  substr(areaid,1,4)||'00' from TB_Hospital where hospitalid='{1}') or areaid='{2}')", ProcureCatalogID, HospitalID, System.Configuration.ConfigurationSettings.AppSettings.Get("CURRENT_DEPLOY_AREAID"));
            HZ.Data.PageDescribe des = new HZ.Data.PageDescribe(sql, pagesize);
            des.CurrentPage = currentpage;
            totalrecord = des.TotalCount;
            return HZ.Data.DAL_Helper.CommonFillList<HZ.DrugTrade.Model.View.AreaDistribution>(des.GetSpecailPage());
        }
    }
}
